let Controller = {
	mounted() {
		this.initData();
	},
	activated() {
		this.initData();
		this.requestDetail();
		this.onDownloadExcel();
	},
	methods: {
		initData() {
			const param = JSON.parse(this.$route.query.data);
			this.Model.queryData = _.cloneDeep(param);
			if (param.id) {
				this.Model.isNew = false;
			} else {
				this.Model.isNew = true;
				this.Model.path[3] = '编辑采购入库';
			}

		},
		onDownloadExcel() {
			this.$ajax.get(this.apiTest + `/supplier/whWarehouseDetail/getGoodsExcelInfo`)
				.then(res => {
					const data = res.data;
					if (data.status == 200) {
						this.Model.fileUrl = data.result.fileUrl;
						this.Model.fileName = data.result.fileName;
					}
				})
		},
		onSelectChange(type) {
			const data = this.Model.sliderSelect[type];
			this.Model.sliderSelect[type].sliderVal = [data.value, data.value];
		},
		goBack() {
			this.$router.back(-1);
		},
		toggleDialog(status) {
			if (!status) {
				this.Model.filterOption2.items.forEach(item => {
					item.data = undefined;
				})
			}
			this.Model.changeGoodsDialogFlag = status;
		},

		requestDetail() {
			if (!this.Model.isNew) {
				const number = this.Model.queryData.id;
				const param = {
					number
				}
				this.$ajax
					.post(this.apiTest + `/supplier/whWarehouseDetail/wareHouseDetail`, param)
					.then(response => {
						const data = response.data;
						if (data.status == 200) {
							const result = data.result;
							const option = result.supplierFactoryLocationList.map(item => {
								return {
									label: item.theNameOfThePlace + '-' + item.descriptionOfWarehouseLocation,
									value: item.id
								}
							})
							this.Model.formData = {
								options: option,
								location: result.inWarehouseName,
								supplier: result.supplierName,
								remark: result.remark,
							};
							this.Model.formData.number = result.originNumber;
							this.Model.tableData = result.actualSaleGoodsInfoVos;
							this.Model.whWarehouseId = result.whWarehouseId;
						} else {
							this.$message.error(data.msg)
						}
					})
					.catch(err => {
						this.$message.error('请求错误');
					});
			}
		},

		tableDetail(row) {
			this.Model.detailDialogFlag = true;
		},
		warehousing(row) {
			this.Model.warehousingDialogStatus = row.status;
			this.Model.warehousingDialogFlag = true;
		},
		onExportMaterial() {
			this.Model.dialogFormVisible = true;
		},
		onSureSearchNumber() {
			const number = this.Model.formData.number;
			if (number) {
				this.$ajax.post(this.apiTest + `/supplier/whWarehouse/getDataFromNumber`, {
						number
					})
					.then(res => {
						const data = res.data;
						if (data.status == 200) {
							const result = data.result;
							this.Model.formData.supplier = result.supplierName;
							let location = [];
							for (let i in result.location) {
								location.push({
									value: i,
									label: result.location[i]
								})
							};
							this.Model.formData.options = location;
						} else {
							this.$message.error(data.msg)
						}
					})
			}
		},
		onFormClearAll() {
			this.Model.formData = _.cloneDeep(this.Model.defaultFormData);
			this.Model.exportBarcode = undefined;
			this.Model.tableData = undefined;
		},
		onSearchExportBarcode() {
			let param = param = {
				keyWord: this.Model.exportBarcode,
				number: this.Model.formData.number
			};
			/* if (this.Model.isNew) {
			  param = {
			    keyWord: this.Model.exportBarcode,
			    number: this.Model.formData.number
			  }
			} else {
			  param = {
			    keyWord: this.Model.exportBarcode,
			    whWarehouseId: this.Model.whWarehouseId
			  }
			} */
			this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/getByKeyWord`, param)
				.then(res => {
					const data = res.data;
					if (data.status == 200) {
						const result = data.result;
						this.Model.tableData.push(result);
					} else {
						this.$message.error(data.msg)
					}
				}).catch(() => {
					this.$message.error('查询失败')
				})
		},
		onListDelete() {
			const list = _.cloneDeep(this.Model.selectList);
			const tableList = _.cloneDeep(this.Model.tableData);
			if (this.Model.isNew) {
				for (let i = 0; i < tableList.length; i++) {
					for (let j = 0; j < list.length; j++) {
						if (tableList[i].id == list[j].id) {
							tableList[i] = undefined;
						}
					}
				};
				this.Model.tableData = tableList.filter(item => {
					return item = item;
				});
				return;
			}
			if (list.length > 0) {
				this.$ajax.post(this.apiTest + `/supplier/whWarehouse/deleteList`, list)
					.then(res => {
						const data = res.data;
						if (data.status == 200) {
							console.log(data);
						}
					}).catch(err => {

					})
			}
		},
		onListSave(type) {
			const list = this.Model.tableData;
			if (list.length > 0) {
				let condition = {};
				for (let i = 0; i < list.length; i++) {
					condition[list[i].batchNo] = list[i].inQuantity;
				}
				if(!this.Model.formData.location){
					this.$message.warning('请选择入库仓库！');
					return;
				}
				const param = {
					condition,
					number: this.Model.formData.number,
					locationId: this.Model.formData.location,
					remark: this.Model.formData.remark,
					saveOrIn: type,
					flag: this.Model.isNew ? 0 : 1,
					whWarehouseId: this.Model.whWarehouseId,
				}
				this.$ajax.post(this.apiTest + `/supplier/whWarehouse/saveList`, param)
					.then(res => {
						const data = res.data;
						if (data.status == 200) {
							this.$router.push({
								name: `purchaseWarehousing`,
							})
							this.$message({
								message: '保存成功!',
								type: 'success'
							});
						} else {
							this.$message.error(data.msg)
						}
					}).catch(err => {

					})
			}

		},
		onUploadFileChange(e) {
			if (e.target.files.length == 0) {
				return;
			}
			this.Model.uploadFile = e.target.files[0];
		},
		onUploadSureFile() {
			/*  */
			const file = this.Model.uploadFile;
			let fileName;
			if (file && !file.name) {
				this.$message({
					type: 'error',
					message: '还没有选择要上传的文件!'
				});
				return;
			} else {
				const extension = file.name.split('.')[1] === 'xls';
				const extension2 = file.name.split('.')[1] === 'xlsx';
				const isLt2M = file.size / 1024 / 1024 < 5
				if (!extension && !extension2) {
					this.$message.warning('上传模板只能是 xls、xlsx格式!')
					return;
				}
				fileName = file.name;
			}
			let fileFormData = new FormData();
			fileFormData.append('file', file, fileName); //filename是键，file是值，就是要传的文件，test.zip是要传的文件名
			let requestConfig = {
				headers: {
					'Content-Type': 'multipart/form-data'
				},
			}
			this.$ajax.post(this.apiTest + `/supplier/whWarehouseDetail/importExcelList`, fileFormData, requestConfig)
				.then(res => {
					const data = res.data;
					if (data.status == 200) {
						this.$message({
							message: '上传成功',
							type: 'success',
						});
						this.Model.tableData = data.result;
						this.Model.dialogFormVisible = false;
					} else {
						this.$message.error(data.msg)
					}
				})
		},
		handleSelectionChange(val) {
			this.Model.selectList = val.map(item => {
				return item.id;
			})
		},
		handleCurrentChange(val) {
			this.Model.pager.params.pageIndex = val;
		},
		handleSizeChange(val) {
			this.Model.pager.params.pageSize = val;
		},
		cardClick(color) {
			this.Model.cardSelected = color;
		},
		showSureBarCode(val) {
			this.Model.searchFromData = val;
		},
		showNotSureBarCodeP1(val, select, opt) {
			if (select.key == 'aSeriesOf') {
				this.findStyleASeriesOf(select.val);
			} else if (select.key == 'styleId') {
				this.findDataFromStyleNumber(select.val);
			}
			this.Model.filterOption2.items = _.cloneDeep(opt);
			this.Model.searchFromData2 = val;
		},
		showNotSureBarCodeP2(val) {
			this.Model.searchFromData3 = val;
		},
		toSearchSureBarCode(val) {},
		toSearchNotSureBarCode(val) {},
		toClear(val) {
			this.Model.searchFromData2 = this.Model.defaultSearchFromData2;
			this.Model.searchFromData3 = this.Model.defaultSearchFromData3;
		},
		toExport(val) {},
		onTabChange(val) {
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		toggleTableDetail(val) {
			const obj = this.Model[val];
			if (obj.flag === true) {
				this.Model[val].src = require('@/assets/img/icon_collapse.png');
				this.Model[val].flag = false;
			} else {
				this.Model[val].src = require('@/assets/img/icon_unfold.png');
				this.Model[val].flag = true;
			}
		},
		onDialogEdit(flag, val) {
			this.Model.editDialogFlag = flag;
			if (flag === false) return;
			const obj = this.Model[val];
			this.Model.editDialogFormData = _.cloneDeep(this.Model.defaultEditDialogFormData);
			this.getAddress();
		},
		changeAddress(val) {
			if (!val) {
				this.getAddress();
			}
		},
		getAddress() {
			const idCard = this.Model.idCard;
			this.$ajax.post(this.apiTest + `/sale/saleOrder/getAddressByIdCard/${idCard}`, {})
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.Model.addressList = data.result;
					}
				}).catch(err => {})
		},
		updateBaseSaleOrder() {
			const orderId = this.Model.queryData.id;
			const data = {
				...this.Model.editDialogFormData,
				orderId,
				idCard: this.Model.idCard,
			};
			let params;
			if (data.shippingMethod === '1') {
				params = {
					orderId,
					idCard: this.Model.idCard,
					shippingMethod: data.shippingMethod,
					shipper: data.shipper,
					originChannel: data.originChannel,
					salesman: data.salesman,
				}
			} else {
				if (this.Model.editDialogNewAddress == false) {
					const index = this.Model.editDialogItemAddressBoxRadio;
					const list = this.Model.addressList;
					data.recipientName = list[index].name;
					data.recipientPhone = list[index].phone;
					data.detailedAddress = list[index].address;
				}
				params = data;
			}
			this.$ajax.post(this.apiTest + `/sale/saleOrder/updateBaseSaleOrder`, params)
				.then(response => {
					const data = response.data;
					if (data.status == 200) {
						this.onDialogEdit(false);
						this.$message({
							type: 'success',
							message: '已成功保存'
						});
						this.requestDetail();
					} else {
						this.$message.error(data.msg);
					}
				}).catch(err => {
					this.$message.error(err);
					this.onDialogEdit(false);
				});
		},

		onSearchSureBarCode() {
			const data = this.Model.filterOption;
			const params = {
				condition: {
					"batchNumber": data.batchNumber || undefined,
					"customCoding": data.customCoding || undefined,
				},
			};
			this.$ajax
				.post(this.apiTest + `/sale/saleOrder/getGoodsByCode`, params)
				.then(response => {
					const data = response.data;
					let result = data.result.result;
					if (data.status == 200) {
						result.forEach(item => {
							return item.goodStatusFlag = true;
						});
						this.Model.barCodeTable = result;
						this.Model.pager.params.pageSize = data.pageSize;
						this.Model.pager.params.pageIndex = data.pageIndex;
						this.Model.pager.pageTotal = data.pageTotal;
					}
				})
				.catch(error => {
					this.$message.error('请求错误');
				});
		},
		onSearchNoCode() {
			const slider = this.Model.sliderSelect.size.sliderVal;
			const params = {
				condition: {
					...this.Model.searchFromData2,
					...this.Model.searchFromData3,
					startInsert: slider[0],
					endInsert: slider[1],
				}
			};
			const condition = params.condition;
			this.$ajax.post(this.apiTest + `/sale/saleOrder/customListBySearch`, params)
				.then(response => {
					const data = response.data;
					let result = data.result.result;
					if (data.status == 200) {
						result.forEach(item => {
							return item.goodStatusFlag = false;
						})
						this.Model.barCodeTable = result;
						this.Model.pager.params.pageSize = data.pageSize;
						this.Model.pager.params.pageIndex = data.pageIndex;
						this.Model.pager.pageTotal = data.pageTotal;
					} else {
						this.$message.error(data.msg);
					}
				}).catch(err => {
					this.$message.error('请求错误');
				})
		}

	}
}
export default Controller;
